﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fixx.Connector;

namespace FixxVS
{
	public partial class Settings : Form
	{
		public enum OpenTo
		{
			ConnectionSettings = 0,
			FilterSettings = 1
		}
		private OpenTo _OpenTo { get; set; }
		private FixxRepositoryCache? _ClearCache { get; set; }

		public FixxPersistantStorage Store { get; set; }
		public bool ConnectionSettingsChanged { get; set; }
		public bool ColumnSettingsChanged { get; set; }
		

		public Settings()
		{
			this._OpenTo = OpenTo.ConnectionSettings;
			InitializeComponent();
		}
		public Settings(OpenTo openTo)
		{
			this._OpenTo = openTo;
			InitializeComponent();
		}

		private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
		{
			var control = (TreeView)sender;
			if (control.SelectedNode.Tag == null)
			{
				switch (control.SelectedNode.Text)
				{
					case "Connection":
						var c = new ConnectionSettings();
						c.Change += this.ConnectionSettings_Change;
						control.SelectedNode.Tag = c;
						break;
					case "Filter":
						var fs = new FilterSettings();
						fs.Change += this.FilterSettings_Change;
						control.SelectedNode.Tag = fs;
						break;
					case "Columns":
						var cols = new ColumnSettings();
						cols.Change += this.ColummSettings_Change;
						control.SelectedNode.Tag = cols;
						break;
					case "Display":
						var dis = new DisplaySettings();
						dis.Change += this.DisplaySettings_Change;
						control.SelectedNode.Tag = dis;
						break;
					case "Cache":
						var cache = new CacheSettings();
						cache.Change += this.CacheSettings_Change;
						control.SelectedNode.Tag = cache;
						break;
					default:
						this.pnlContainer.Controls.Clear();
						return;
				}
			}
			var uc = (UserControl)control.SelectedNode.Tag;
			uc.Dock = DockStyle.Fill;
			uc.Visible = true;
			this.pnlContainer.Controls.Clear();
			this.pnlContainer.Controls.Add(uc);

		}

		private void ConnectionSettings_Change(object sender, ConnectionSettingsChangeArgs e)
		{
			this.ConnectionSettingsChanged = true;
			this.MarkInvalid(!e.Valid);
			if (e.Valid)
			{
				this.Store.Repository = new FixxRepository(
					new FixxVSSettings(e.Username,e.Password,e.Host,e.Port,e.TimeOut * 1000)
				);

				if (e.Changed)
					this.Store.Repository.ClearCache(FixxRepositoryCache.All);

			}
			else
			{
				if (this.Store != null)
					this.Store.Repository = null;
			}
		}
		private void FilterSettings_Change(object sender, FilterSettingsChangeArgs e)
		{
			if (this.Store == null || this.Store.Repository == null)
			{
				this.MarkInvalid(false);
				this.tvOptions.SelectedNode = this.tvOptions.Nodes["nodeConnection"];
			}
			else
			{
				var fs = this.Store.Repository.FixxSettings;
				fs.OnlyIssuesAssignedToMe = e.OnlyAssignedToMe;
				fs.IncludeArchivedProjects = e.IncludeArchivedProjects;
				fs.IncludeClosedIssues = e.IncludeClosedIssues;
				fs.IncludeResolvedIssues = e.IncludeResolvedIssues;
			}
		}
		private void ColummSettings_Change(object sender, ColumnSettingsChangeArgs e)
		{
			if (this.Store == null || this.Store.Repository == null)
			{
				this.MarkInvalid(false);
				this.tvOptions.SelectedNode = this.tvOptions.Nodes["nodeConnection"];
			}
			else
			{
				var fs = (FixxVSSettings)this.Store.Repository.FixxSettings;
				fs.IssueListviewColumnConfig = e.IssueListviewColumnConfig;
				this.ColumnSettingsChanged = true;
			}
		}
		private void DisplaySettings_Change(object sender, DisplaySettingsChangeArgs e)
		{
			if (this.Store == null || this.Store.Repository == null)
			{
				this.MarkInvalid(false);
				this.tvOptions.SelectedNode = this.tvOptions.Nodes["nodeConnection"];
			}
			else
			{
				var fs = (FixxVSSettings)this.Store.Repository.FixxSettings;
				fs.IsUsingPretyDateFormater = e.IsUsingPretyDateFormater;
				this.ColumnSettingsChanged = true;
			}
		}
		private void CacheSettings_Change(object sender, CacheSettingsChangeArgs e)
		{
			this._ClearCache = e.CacheItems;
		}

		private void Settings_Load(object sender, EventArgs e)
		{
			this.Store = new FixxPersistantStorage();
			this.tvOptions.ExpandAll();
			if (this.Store.Repository == null)
			{
				this.MarkInvalid(true);
			}
			else
			{
				this.MarkInvalid(false);
				if (this._OpenTo == OpenTo.ConnectionSettings)
					this.tvOptions.SelectedNode = this.tvOptions.Nodes["nodeConnection"];
				if (this._OpenTo == OpenTo.FilterSettings)
					this.tvOptions.SelectedNode = this.tvOptions.Nodes["nodeFilter"];
			}
		}

		private void MarkInvalid(bool invalid)
		{
			foreach (TreeNode node in this.AllOptionNodes())
			{
				node.ForeColor = (invalid) ? Color.Gray : Color.Black;
			}
			this.tvOptions.Nodes["nodeConnection"].ForeColor = Color.Black;
			if (invalid)
				this.tvOptions.SelectedNode = this.tvOptions.Nodes["nodeConnection"];
			this.btnOk.Enabled = !invalid;
			this.btnApply.Enabled = !invalid;
			
		}

		private List<TreeNode> AllOptionNodes()
		{
			return new List<TreeNode>()
			{
				this.tvOptions.Nodes["nodeFilter"],
				this.tvOptions.Nodes["nodeConnection"],
				this.tvOptions.Nodes["nodeConnection"].Nodes["nodeCache"],
				this.tvOptions.Nodes["nodeDisplay"],
				this.tvOptions.Nodes["nodeDisplay"].Nodes["nodeColumns"],
				this.tvOptions.Nodes["nodeDisplay"].Nodes["nodeColours"]
			};

		}

		private void tvOptions_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (e.Node.ForeColor == Color.Gray)
			{
				this.tvOptions.SelectedNode = this.tvOptions.Nodes["nodeConnection"];
				e.Cancel = true;
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this._ClearCache.HasValue)
				this.Store.Repository.ClearCache(this._ClearCache.Value);
			
			this.Store.Save();
			this.DialogResult = DialogResult.OK;
		}
	}
}
